package lambdasinaction.chap8;

import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;


public class Peek {

    public static void main(String[] args) {

        /**
         * peek 生成一个包含原Stream的所有元素的新Stream，同时会提供一个消费函数（Consumer实例），新Stream每个元素被消费的时候都会执行给定的消费函数；
         *   结果：
         *   taking from stream: 2
             after map: 19

             taking from stream: 3
             after map: 20
             after filter: 20
             after limit: 20

             taking from stream: 4
             after map: 21

             taking from stream: 5
             after map: 22
             after filter: 22
             after limit: 22
         */
        List<Integer> result = Stream.of(2, 3, 4, 5)
                .peek(x -> System.out.println("taking from stream: " + x)).map(x -> x + 17)
                .peek(x -> System.out.println("after map: " + x)).filter(x -> x % 2 == 0)
                .peek(x -> System.out.println("after filter: " + x)).limit(3)
                .peek(x -> System.out.println("after limit: " + x)).collect(toList());

        Stream.generate(Math::random).limit(10).forEach(System.out::println);
        Stream.iterate(1, x -> x + 2).limit(10).parallel().forEach(System.out::println);
    }
}
